package com.powerteam.controller.masterData;

import com.github.pagehelper.PageInfo;
import com.powerteam.controller.AuthorizedController;
import com.powerteam.model.masterData.OrgUnit;
import com.powerteam.model.masterData.OrgUnitUser;
import com.powerteam.model.sys.User;
import com.powerteam.service.masterData.OrgUnitService;
import com.powerteam.vo.Result;
import com.powerteam.vo.masterData.QueryOrgUnitUserVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 组织单位相关接口
 *
 * @author jinming.zhang
 * @version 0.0.1
 * @date 2021/12/30
 */
@Slf4j
@Controller
@RequestMapping("/orgUnit")
public class OrgUnitController extends AuthorizedController {

    @Autowired
    private OrgUnitService orgUnitService;

    /**
     * 组织架构页
     *
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    public String index() {

        return "masterData/orgUnit";
    }

    /**
     * 查找所有组织单位
     *
     * @param
     * @return List<OrgUnit>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/findAllOrgUnit", method = RequestMethod.POST)
    public List<OrgUnit> findAllOrgUnit() {

        return orgUnitService.findAllOrgUnit();
    }

    /**
     * 添加组织架构信息
     *
     * @param orgUnit
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result add(@RequestBody OrgUnit orgUnit) {

        return orgUnitService.insert(orgUnit);
    }

    /**
     * 修改组织架构信息
     *
     * @param orgUnit
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody OrgUnit orgUnit) {

        return orgUnitService.update(orgUnit);
    }

    /**
     * 删除组织架构信息
     *
     * @param orgUnit
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/remove", method = RequestMethod.POST)
    public Result remove(@RequestBody OrgUnit orgUnit) {

        return orgUnitService.delete(orgUnit.getOrgUnitId());
    }

    /**
     * 上调组织架构信息
     *
     * @param orgUnit
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/up", method = RequestMethod.POST)
    public Result up(@RequestBody OrgUnit orgUnit) {

        return orgUnitService.up(orgUnit.getOrgUnitId());
    }

    /**
     * 下调组织架构信息
     *
     * @param orgUnit
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/down", method = RequestMethod.POST)
    public Result down(@RequestBody OrgUnit orgUnit) {

        return orgUnitService.down(orgUnit.getOrgUnitId());
    }

    /**
     * 分配用户
     *
     * @param
     * @return String
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @RequestMapping(value = "/orgUser", method = RequestMethod.GET)
    public String orgUser() {

        return "masterData/orgUser";
    }

    /**
     * 分页查找不在该组织单位中的用户
     *
     * @param vo
     * @return PageInfo<User>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/orgUser/findUserNotInOrgUnit", method = RequestMethod.POST)
    public PageInfo<User> findUserNotInOrgUnit(@RequestBody QueryOrgUnitUserVO vo) {

        return orgUnitService.findUserNotInOrgUnit(vo);
    }

    /**
     * 分页查找该组织单位中的用户
     *
     * @param vo
     * @return PageInfo<OrgUnitUser>
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/orgUser/findUserInOrgUnit", method = RequestMethod.POST)
    public PageInfo<OrgUnitUser> findUserInOrgUnit(@RequestBody QueryOrgUnitUserVO vo) {

        return orgUnitService.findUserInOrgUnit(vo);
    }

    /**
     * 将用户添加到组织单位
     *
     * @param orgUnitUser
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/orgUser/add", method = RequestMethod.POST)
    public Result addOrgUser(@RequestBody OrgUnitUser orgUnitUser) {

        return orgUnitService.insertOrgUnitUser(orgUnitUser);
    }

    /**
     * 将用户从该组织单位移除
     *
     * @param orgUnitUser
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/orgUser/remove", method = RequestMethod.POST)
    public Result removeOrgUser(@RequestBody OrgUnitUser orgUnitUser) {

        return orgUnitService.deleteOrgUnitUser(orgUnitUser);
    }

    /**
     * 将用户设置为主管
     *
     * @param orgUnitUser
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/orgUser/setUnitHead", method = RequestMethod.POST)
    public Result setUnitHead(@RequestBody OrgUnitUser orgUnitUser) {

        return orgUnitService.setUnitHead(orgUnitUser);
    }

    /**
     * 移除当前组织单位的主管
     *
     * @param orgUnitUser
     * @return Result
     * @Author jinming.zhang
     * @Date 2022/1/10
     */
    @ResponseBody
    @RequestMapping(value = "/orgUser/removeUnitHead", method = RequestMethod.POST)
    public Result removeUnitHead(@RequestBody OrgUnitUser orgUnitUser) {

        return orgUnitService.removeUnitHead(orgUnitUser);
    }

}
